using System;
using Common.Enumerations;
using Sanelib.Common.Extensions;
using Sanelib.Common.Helpers;
using Sanelib.Dto;

namespace Common.Commands.ContactCommands
{
    public class AddContact : AuditedCommand
    {
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public NameTitle NameTitle { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Mobile { get; set; }
        public string CompanyName { get; set; }
        public Designation Designation { get; set; }

        public override Guid? GetAggregateId()
        {
            return null;
        }

        public override ErrorResult Validate()
        {
            var validationResult = new ErrorResult();

            if (!string.IsNullOrWhiteSpace(Name) && Name.Length > 256)
                validationResult.AddError("Name", " entered exceeds the maximum length 256");

            if (string.IsNullOrWhiteSpace(Name))
                validationResult.AddError("Name", "should not be empty");

            if (NameTitle == null)
                validationResult.AddError("Salutation", "should select.");

            if (!string.IsNullOrWhiteSpace(FirstName) && FirstName.Length > 128)
                validationResult.AddError("First Name", " entered exceeds the maximum length 128");

            if (string.IsNullOrWhiteSpace(FirstName))
                validationResult.AddError("First Name", "should not be empty");

            if (!string.IsNullOrWhiteSpace(LastName) && LastName.Length > 128)
                validationResult.AddError("Last Name", " entered exceeds the maximum length 128");

            if (string.IsNullOrWhiteSpace(LastName))
                validationResult.AddError("Last Name", "should not be empty");

            if (!string.IsNullOrWhiteSpace(Email) && Email.Length > 128)
                validationResult.AddError("Email", " entered exceeds the maximum length 128");

            if (Email.IsEmpty())
                validationResult.AddError("Email", "should not be empty");
            else
            {
                if (Email.IsNotEmpty() && Formatter.EmailId(Email) != true)
                    validationResult.AddError("Email", "should be correct formate. (xxxxx@xxx.xx)");
            }

            if (!string.IsNullOrWhiteSpace(Phone) && Phone.Length > 16)
                validationResult.AddError("Phone", " entered exceeds the maximum length 16");

            if (!string.IsNullOrWhiteSpace(Mobile) && Mobile.Length > 128)
                validationResult.AddError("Mobile", " entered exceeds the maximum length 16");

            if (!string.IsNullOrWhiteSpace(CompanyName) && CompanyName.Length > 128)
                validationResult.AddError("Company Name", " entered exceeds the maximum length 128");

            if (Designation == null)
                validationResult.AddError("Designation", "should select.");

            if (!string.IsNullOrWhiteSpace(Description) && Description.Length > 2048)
                validationResult.AddError("Description", " entered exceeds the maximum length 2048");

            return validationResult;
        }
    }
}
